package top.wangxiaomei.ai.other;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public class TreeNode<T> {

    T data;
    // 获取子节点
    List<TreeNode<T>> children;

    public TreeNode(T data) {
        this.data = data;
        this.children = new ArrayList<>();
    }

    // 添加子节点
    public void addChild(TreeNode<T> child) {
        this.children.add(child);
    }

    // 打印树节点及其子节点
    public String printTree(String prefix) {
        StringBuilder prefixStr = new StringBuilder(prefix + data.toString());
        prefixStr.append("<br />");
        for (TreeNode<T> child : children) {
            prefixStr.append(child.printTree(prefix + "--"));
        }
        return prefixStr.toString();
    }
}
